const app = getApp()
Page({
    data: {
      config: app.config,
      
      uid: '',
      planePhone: '',
      loginShow: false,
      login:false,

      params: [
        {
          activityId: '2022051100826004950115286127',
          outBizNo: 'adeb91bb023041279da5d03d5e86c977',
        },
      ],
      paramsCopy: [],
      senderMerchantId: '2088341688201953',
      dialogBtnType: 1,
      dialogBtnStyle: {
        color: '#fff',
        borderColor: '#1664FF',
        backgroundColor: '#1664FF',
      },

    },
    onLoad() {
        app._init_(this)
        this.getHome();
        
        this.getTicket();

        this.setNamePhone();
    },


    definedGoPage(e){
      let item = e.target.dataset.item;
      console.log(999,item);
      if(item.router){
        my.navigateTo({
          url: item.router + '?param=' + item.id + '&comePath=ticket'
        })
      }else{
        my.navigateToMiniProgram({
          appId: item.outAppid,
          path: item.outRouter + '?param=' + item.id + '&comePath=ticket',
          success: (res) => {
            console.log(111,JSON.stringify(res))
          },
          fail: (res) => {
            console.log(222,JSON.stringify(res))
          },
          complete:(res) => {
              console.log(333,JSON.stringify(res))
          }
        });
      }
    },


    // 获取活动
    getTicket(){
      let arr = [];
      app.other.queryActivity()
      .then(res=>{
        console.log(777,res);
        if(res.code == 200 && res.data.length>0){
          res.data.forEach(item=>{
            let obj = {
              activityId: item.activityId,
              outBizNo: item.outBizNo,
            }
            arr.push(obj);
          })
        }
      })

      this.setData({
        paramsCopy: arr
      })

    },

    onLgin(data){
      console.log(111,data)
      this.setData({
        login: data.login,
        loginShow: data.loginShow
      });
    },

    onBeforeGetCoupon(){

      // console.log(999,this.data.params);

      if(!this.data.login){
        this.setData({
          params:{}
        })
        my.showToast({
          content: '请先登录！'
        })

        setTimeout(() => {
          this.setData({
            loginShow:true
          })
        }, 1500);

      }else{
        this.setData({
          params: this.data.paramsCopy
        })
        if(this.data.paramsCopy.length<=0){
          my.showToast({
            content: '活动已经结束了哦~'
          });
        }
      }
    },

    onGetCouponSuccess(resultList, { extraData }) {
      // console.log('触发了 onGetCouponSuccess 事件')
      console.log('成功返回结果: ', resultList)

      // let arr = [];
      // resultList.forEach(item=>{
      //   arr.push(item.voucherCode);
      // })

      let voucherCode = resultList[0].voucherCode

      let dat = {
        uid: this.data.uid,
        couponStatus: 1,
        voucherCode: voucherCode
      }

      // console.log(556,dat)

      app.other.merchantVoucherEdit(dat)
      .then(res=>{
        // console.log(34567,res)
      })

    },
    onGetCouponFail(result, { extraData }) {
      // console.log('触发了 onGetCouponFail 事件')
      console.log('失败返回结果: ', result)
    },
    onUseImmediately(event, { extraData }) {
      // console.log('触发了 onUseImmediately 事件')
      // 可以跳转到自定义的页面
      // my.navigateTo({
      //   url: '/pages/goods-detail/index',
      // })
    },
    onClose(event, { extraData }) {
      // console.log('触发了 onClose 事件')
    },


    setNamePhone(){
      let user = my.getStorageSync({ key: 'user' });
      let uid = user.data;
      let planePhone = app.globalData.planePhone;
      this.setData({
        planePhone: planePhone,
      })
      if(uid){
        this.setData({
          uid: uid.id,
          login: true
        })
      }

    },

    getHome(){
        app.other.home()
        .then(res=>{
            const data = JSON.parse(res.data.bigType).data.ticket
            console.log(666,data)
            this.setData(data)
        })
    },
    // 跳转
    navigateTo(e){
        app.navigateTo(e)
    }
});
